/*
 * Created on 2006-7-5
 */
package mathUtil;

import java.awt.geom.Point2D;
import java.util.ArrayList;

import entity.Velocity;

public class CoefficientsQuartic {

    double x1, y1, v1x, v1y, x2, y2, v2x, v2y, x0, y0, R;

    double a, b, c, d, e, f, g, h;
    
    double k,l,m;
    
    double A,B,C,D,E;

    public CoefficientsQuartic(Point2D focalLoc, Velocity focalSpeed,
    		Point2D coLoc, Velocity coSpeed, Point2D cellCenter, double Radius) {
        this.x1 = focalLoc.getX();
        this.y1 = focalLoc.getY();
        this.v1x = focalSpeed.getVx();
        this.v1y = focalSpeed.getVy();
        this.x2 = coLoc.getX();
        this.y2 = coLoc.getY();
        this.v2x = coSpeed.getVx();
        this.v2y = coSpeed.getVy();
        this.x0 = cellCenter.getX();
        this.y0 = cellCenter.getY();
        this.R = Radius;
    }
    
    public ArrayList coefficients(){
        a = x1-x2;
        b = v1x-v2x;
        c=y1-y2;
        d=v1y-v2y;
        e=x1-x0;
        f=y1-y0;
        g=v1x;
        h=v1y;
        
        k=g*g+h*h-b*b-d*d;
        l=2*e*g+2*f*h-2*a*b-2*b*c;
        m=e*e+f*f+R*R-a*a-c*c;
        
        A=k*k;
        B=2*k*l;
        C=2*m*k+l*l-4*R*R*(g*g+h*h);
        D= 2*m*l -4*R*R*(2*e*g+2*f*h);
        E=m*m-4*R*R*(e*e+f*f);
        
        ArrayList cos = new ArrayList();
        cos.add(0,new Double(A));
        cos.add(1,new Double(B));
        cos.add(2,new Double(C));
        cos.add(3,new Double(D));
        cos.add(4,new Double(E));
        
        return cos;
    }
	/**
	 * @param a
	 * @param b
	 * @param c
	 * @return null if no root; otherwise an ArrayList which contains the
	 *         root(s)
	 */
	public ArrayList<Double> quadratics(double a, double b, double c) {
		ArrayList<Double> roots = new ArrayList<Double>();
		if (a == 0) {
			if (b == 0) {
				return null;
			} else {
				roots.add(new Double(-1 * c / b));
				return roots;
			}
		} else {
			double delta = (b * b - 4 * a * c);
			if (delta > 0) {
				double root1 = (-1 * b + Math.sqrt(delta)) * 1.01
						/ (2 * a * 1.01);
				double root2 = (-1 * b - Math.sqrt(delta)) * 1.01
						/ (2 * a * 1.01);
				roots.add(new Double(root1));
				roots.add(new Double(root2));
				return roots;
			} else if (delta == 0) {
				double root = (-1 * b * 1.01 / (2 * a * 1.01));
				roots.add(new Double(root));
				return roots;
			} else {
				return null;
			}
		}
	}
}
